﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using shuatibao.Tool;

namespace shuatibao.Web.Chapter
{
    public class ChapterAction
    {
        #region 章节
        #region 添加章节
        public void insertChapter(string partid, string chapterName,string rowindex)
        {
            // 连接数据库
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();
            string StrInsert = "";

            StrInsert = "insert into Chapter (name,parent_id,rowindex) values (@name,@parent_id,@rowindex)";
            SqlCommand cmd = new SqlCommand(StrInsert, sqlcon);
            // 添加参数并且设置参数值

            cmd.Parameters.Add("@name", SqlDbType.VarChar);
            cmd.Parameters["@name"].Value = chapterName;
            cmd.Parameters.Add("@parent_id", SqlDbType.VarChar);
            cmd.Parameters["@parent_id"].Value = partid;
            cmd.Parameters.Add("@rowindex", SqlDbType.VarChar);
            cmd.Parameters["@rowindex"].Value = rowindex;

            // 执行插入数据的操作
            cmd.ExecuteNonQuery();
            sqlcon.Close();
        }
        #endregion

        #region 修改章节
        public void updateChapterManager(string id, string name)
        {
            //连接数据库
            SqlConnection sqlcon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon1.Open();
            //修改数据信息
            string strSqls = "";

            strSqls = "update Chapter set name = '" + name + "' where id = '" + id + "'";

            SqlCommand cmd = new SqlCommand(strSqls, sqlcon1);
            //添加参数并且设置参数值
            cmd.ExecuteNonQuery();
            sqlcon1.Close();
        }
        #endregion

        #region 获取章节列表
        public List<ChapterModel> getChapterList(string partId)
        {
            string sqlStr = "select  * from Chapter where parent_id = '" + partId + "'";


            DataRowCollection dataRows = SQLTool.sqlGetListManager(sqlStr);
            int rows = dataRows.Count;

            List<ChapterModel> chapterList = new List<ChapterModel>();

            for (int i = 0; i < rows; i++)
            {
                ChapterModel subjectModel = new ChapterModel();
                subjectModel.id = dataRows[i]["id"].ToString();
                subjectModel.name = dataRows[i]["name"].ToString();
                subjectModel.rowindex = dataRows[i]["rowindex"].ToString();

                chapterList.Add(subjectModel);
            }

            return chapterList;

        }
        #endregion

        #region 删除章节列表
        public void deleteChapter(string id)
        {
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
            sqlcon.Open();
            string StrInsert = "";
            StrInsert = "delete from Chapter where id = '" + id + "'";
            SqlCommand cmd = new SqlCommand(StrInsert, sqlcon);
            // 添加参数并且设置参数值

            // 执行插入数据的操作
            cmd.ExecuteNonQuery();
            sqlcon.Close();

        }
        #endregion
        #endregion
    }
}